package com.sansan.qiangji;

import com.sansan.qiangji.entity.warehouse.StackerTaskIn;
import com.sansan.qiangji.entity.warehouse.StackerTaskMove;
import com.sansan.qiangji.entity.warehouse.StackerTaskOut;
import com.sansan.qiangji.service.impl.system.PowerServiceImpl;
import com.sansan.qiangji.service.impl.warehouse.StackerTaskInServiceImpl;
import com.sansan.qiangji.service.impl.warehouse.StackerTaskMoveServiceImpl;
import com.sansan.qiangji.service.impl.warehouse.StackerTaskOutServiceImpl;
import com.sansan.qiangji.service.impl.warehouse.TaskLogServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Component
public class ApplicationRunnerImpl implements ApplicationRunner {
    private StackerTaskInServiceImpl stackerTaskInService;
    private StackerTaskOutServiceImpl stackerTaskOutService;
    private StackerTaskMoveServiceImpl stackerTaskMoveService;
    private TaskLogServiceImpl taskLogService;
    private PowerServiceImpl powerService;

    @Resource
    public void setStackerTaskInService(StackerTaskInServiceImpl stackerTaskInService) {
        this.stackerTaskInService = stackerTaskInService;
    }

    @Resource
    public void setStackerTaskOutService(StackerTaskOutServiceImpl stackerTaskOutService) {
        this.stackerTaskOutService = stackerTaskOutService;
    }

    @Resource
    public void setStackerTaskMoveService(StackerTaskMoveServiceImpl stackerTaskMoveService) {
        this.stackerTaskMoveService = stackerTaskMoveService;
    }

    @Resource
    public void setTaskLogService(TaskLogServiceImpl taskLogService) {
        this.taskLogService = taskLogService;
    }

    @Resource
    public void setPowerService(PowerServiceImpl powerService) {
        this.powerService = powerService;
    }

    @Override
    public void run(ApplicationArguments args) {
        log.info("项目初始化");
        List<StackerTaskIn> stackerTaskIns = stackerTaskInService.findByStackerTaskStatus("入库中");
        List<StackerTaskOut> stackerTaskOuts = stackerTaskOutService.findByStackerTaskStatus("出库中");
        List<StackerTaskMove> stackerTaskMoves = stackerTaskMoveService.findByStackerTaskStatus("移库中");
        for (StackerTaskIn stackerTaskIn : stackerTaskIns) {
            stackerTaskIn.setStackerTaskInStatus("中断");
        }
        for (StackerTaskOut stackerTaskOut : stackerTaskOuts) {
            stackerTaskOut.setStackerTaskOutStatus("中断");
        }
        for (StackerTaskMove stackerTaskMove : stackerTaskMoves) {
            stackerTaskMove.setStackerTaskMoveStatus("中断");
        }
        if (stackerTaskIns.size() > 0) {
            stackerTaskInService.updateStackerTaskIns(stackerTaskIns);
        }
        if (stackerTaskOuts.size() > 0) {
            stackerTaskOutService.updateStackerTaskOuts(stackerTaskOuts);
        }
        if (stackerTaskMoves.size() > 0) {
            stackerTaskMoveService.updateStackerTaskMoves(stackerTaskMoves);
        }
        taskLogService.deleteAll();
    }
}
